﻿namespace BTS.COMMON
{
    using System;
    using System.Text;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Xml;

    public abstract class AreaHelper
    {
        private static string xmlPath = (FileHelper.AppPhyRoot + @"CONFIGURATIONS\area.config");

        protected AreaHelper()
        {
        }

        public static string GetCityDataVal(string provinceId, string cityId)
        {
            XmlDocument document = new XmlDocument();
            document.Load(xmlPath);
            XmlNode node = document.SelectSingleNode("area");
            foreach (XmlNode node2 in node.ChildNodes)
            {
                string str = node2.Attributes["provinceID"].Value;
                if (!string.IsNullOrEmpty(provinceId) && (provinceId == str))
                {
                    foreach (XmlNode node3 in node2.ChildNodes)
                    {
                        string str2 = node3.Attributes["CityID"].Value;
                        if (!(string.IsNullOrEmpty(cityId) || !(cityId == str2)))
                        {
                            return node3.Attributes["City"].Value;
                        }
                    }
                }
            }
            return string.Empty;
        }

        public static string GetCityOption(string provinceId)
        {
            StringBuilder builder = new StringBuilder();
            XmlDocument document = new XmlDocument();
            document.Load(xmlPath);
            XmlNode node = document.SelectSingleNode("area");
            if (node.HasChildNodes)
            {
                foreach (XmlNode node2 in node.ChildNodes)
                {
                    string str = node2.Attributes["provinceID"].Value;
                    if (!string.IsNullOrEmpty(provinceId) && (str == provinceId))
                    {
                        foreach (XmlNode node3 in node2.ChildNodes)
                        {
                            builder.Append(node3.Attributes["CityID"].Value).Append("|").Append(node3.Attributes["City"].Value).Append(",");
                        }
                        return builder.ToString();
                    }
                }
            }
            return string.Empty;
        }

        public static string GetPieceareaDataVal(string provinceId, string cityId, string pieceareaId)
        {
            XmlDocument document = new XmlDocument();
            document.Load(xmlPath);
            XmlNode node = document.SelectSingleNode("area");
            foreach (XmlNode node2 in node.ChildNodes)
            {
                string str = node2.Attributes["provinceID"].Value;
                if (!string.IsNullOrEmpty(provinceId) && (provinceId == str))
                {
                    foreach (XmlNode node3 in node2.ChildNodes)
                    {
                        string str2 = node3.Attributes["CityID"].Value;
                        if (!string.IsNullOrEmpty(cityId) && (cityId == str2))
                        {
                            foreach (XmlNode node4 in node3.ChildNodes)
                            {
                                string str3 = node4.Attributes["PieceareaID"].Value;
                                if (!(string.IsNullOrEmpty(pieceareaId) || !(pieceareaId == str3)))
                                {
                                    return node4.Attributes["Piecearea"].Value;
                                }
                            }
                        }
                    }
                }
            }
            return string.Empty;
        }

        public static string GetPieceareaOption(string provinceId, string cityId)
        {
            StringBuilder builder = new StringBuilder();
            XmlDocument document = new XmlDocument();
            document.Load(xmlPath);
            XmlNode node = document.SelectSingleNode("area");
            if (node.HasChildNodes)
            {
                foreach (XmlNode node2 in node.ChildNodes)
                {
                    string str = node2.Attributes["provinceID"].Value;
                    if (!string.IsNullOrEmpty(provinceId) && (str == provinceId))
                    {
                        foreach (XmlNode node3 in node2.ChildNodes)
                        {
                            string str2 = node3.Attributes["CityID"].Value;
                            if (!string.IsNullOrEmpty(cityId) && (str2 == cityId))
                            {
                                foreach (XmlNode node4 in node3.ChildNodes)
                                {
                                    builder.Append(node4.Attributes["PieceareaID"].Value).Append("|").Append(node4.Attributes["Piecearea"].Value).Append(",");
                                }
                                return builder.ToString();
                            }
                        }
                    }
                }
            }
            return string.Empty;
        }

        public static string GetProvDataVal(string provinceId)
        {
            XmlDocument document = new XmlDocument();
            document.Load(xmlPath);
            XmlNode node = document.SelectSingleNode("area");
            foreach (XmlNode node2 in node.ChildNodes)
            {
                string str = node2.Attributes["provinceID"].Value;
                if (!(string.IsNullOrEmpty(provinceId) || !(provinceId == str)))
                {
                    return node2.Attributes["province"].Value;
                }
            }
            return string.Empty;
        }

        public static string GetProvinceOption()
        {
            StringBuilder builder = new StringBuilder();
            XmlDocument document = new XmlDocument();
            document.Load(xmlPath);
            XmlNode node = document.SelectSingleNode("area");
            if (node.HasChildNodes)
            {
                foreach (XmlNode node2 in node.ChildNodes)
                {
                    builder.Append("<option value=\"").Append(node2.Attributes["provinceID"].Value).Append("\">").Append(node2.Attributes["province"].Value).Append("</option>");
                }
            }
            return builder.ToString();
        }

        public static string GetProvinceAll(string formatstr)
        {
            StringBuilder builder = new StringBuilder();
            XmlDocument document = new XmlDocument();
            document.Load(xmlPath);
            XmlNode node = document.SelectSingleNode("area");
            if (node.HasChildNodes)
            {
                foreach (XmlNode node2 in node.ChildNodes)
                {
                    builder.Append(formatstr.Replace("{$id$}", node2.Attributes["provinceID"].Value).Replace("{$value$}", node2.Attributes["province"].Value));
                }
            }
            return builder.ToString();
        }

        public static string GetProvinceByPieceareaID(string provinceId)
        {
            XmlDocument document = new XmlDocument();
            document.Load(xmlPath);
            XmlNode node = document.SelectSingleNode("area");
            if (node.HasChildNodes)
            {
                foreach (XmlNode node2 in node.ChildNodes)
                {
                    if (!string.IsNullOrEmpty(provinceId) && node2.Attributes["provinceID"].Value.Trim()==provinceId.Trim())
                    {
                        return node2.Attributes["province"].Value;
                    }
                }
            }
            return "";
        }

        public static void SetCtrlByCity(HtmlSelect hs, string provinceId, string selectVal)
        {
            XmlDocument document = new XmlDocument();
            document.Load(xmlPath);
            XmlNode node = document.SelectSingleNode("area");
            if (node.HasChildNodes)
            {
                foreach (XmlNode node2 in node.ChildNodes)
                {
                    string str = node2.Attributes["provinceID"].Value;
                    if (!string.IsNullOrEmpty(provinceId) && (str == provinceId))
                    {
                        if (str == "110000")
                        {
                            hs.Items.Add(new ListItem("北京市", "110100"));
                        }
                        else if (str == "120000")
                        {
                            hs.Items.Add(new ListItem("天津市", "120100"));
                        }
                        else if (str == "310000")
                        {
                            hs.Items.Add(new ListItem("上海市", "310100"));
                        }
                        else if (str == "500000")
                        {
                            hs.Items.Add(new ListItem("重庆市", "500100"));
                        }
                        else if (str == "710000")
                        {
                            hs.Items.Add(new ListItem("台湾省", "710100"));
                        }
                        else if (str == "810000")
                        {
                            hs.Items.Add(new ListItem("香港特别行政区", "810100"));
                        }
                        else if (str == "820000")
                        {
                            hs.Items.Add(new ListItem("澳门特别行政区", "820100"));
                        }
                        else
                        {
                            foreach (XmlNode node3 in node2.ChildNodes)
                            {
                                ListItem item = new ListItem();
                                item.Text = node3.Attributes["City"].Value;
                                item.Value = node3.Attributes["CityID"].Value;
                                if (!(string.IsNullOrEmpty(selectVal) || !(selectVal.Trim() == item.Value.Trim())))
                                {
                                    item.Selected = true;
                                }
                                hs.Items.Add(item);
                            }
                        }
                        break;
                    }
                }
            }
        }

        public static void SetCtrlByProv(HtmlSelect hs, string selectVal)
        {
            XmlDocument document = new XmlDocument();
            document.Load(xmlPath);
            XmlNode node = document.SelectSingleNode("area");
            if (node.HasChildNodes)
            {
                foreach (XmlNode node2 in node.ChildNodes)
                {
                    ListItem item = new ListItem();
                    item.Text = node2.Attributes["province"].Value;
                    item.Value = node2.Attributes["provinceID"].Value;
                    if (!(string.IsNullOrEmpty(selectVal) || !(selectVal.Trim() == item.Value.Trim())))
                    {
                        item.Selected = true;
                    }
                    hs.Items.Add(item);
                }
            }
        }
    }
}

